package com.abaddon.controller;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;

import com.abaddon.model.Place;
import com.abaddon.model.Tag;
import com.abaddon.view.Abaddon;
import com.abaddon.view.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class AbaddonItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();
	Abaddon abaddon;
	private static Place swapPlace;
	
	public AbaddonItemizedOverlay(Drawable defaultMarker,Abaddon abaddon) {
		super(boundCenterBottom(defaultMarker));
		this.abaddon = abaddon;
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		if (overlays.isEmpty()){
			return false;
		} else {
		//		if(event.getAction() == MotionEvent.ACTION_DOWN){
		//			GeoPoint gp = mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
		//			abaddon.setTitle("Lat: " + gp.getLatitudeE6() +" Long: " + gp.getLongitudeE6());
		//		}
		//} 
			return super.onTouchEvent(event, mapView);
		}
		
		
	}
	
	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		if (overlays.isEmpty())
			return true;
		else
			return super.onTap(p, mapView);
	}
	
	@Override
	protected boolean onTap(int index) {
		Tag item = (Tag)overlays.get(index);
		if(item instanceof Place){
			Place place = (Place)item;
			swapPlace = place;
			new AlertDialog.Builder(abaddon)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle(R.string.editPlace)
			.setMessage(R.string.editPlaceQuestion)
			.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
				
				@Override
		            public void onClick(DialogInterface dialog, int which) {
						Intent hej = new Intent("com.abaddon.view.EDITPLACEVIEW");
						Place tempPlace = AbaddonItemizedOverlay.swapPlace;
						hej.putExtra("event", "editPlace");
						hej.putExtra("title", tempPlace.getTitle());
						hej.putExtra("snippet", tempPlace.getSnippet());
						hej.putExtra("categories", tempPlace.getCategoriesArray());
						abaddon.startActivity(hej);
		            }
				
			})
			.setNegativeButton(R.string.no, null)
			.show();
		}
		return true;
	}
	
	/**
	 * Adds an overlayItem to the overlayslist.
	 * @param overlayItem Overlaysitem to be added.
	 */
	public void addOverlay(OverlayItem overlayItem){
		overlays.add(overlayItem);
		populate();
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		if (!overlays.isEmpty()){
			return overlays.get(i);
		}
		return null;
	}

	@Override
	public int size() {
		if (!overlays.isEmpty()){
			return overlays.size();
		}
		return 0;
	}
	
	 @Override
	    public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow){
	        super.draw(canvas, mapView, shadow);

	        if (shadow == false) {
	            //cycle through all overlays
	            for (int index = 0; index < overlays.size(); index++) {
	                OverlayItem item = overlays.get(index);

	                // Converts lat/lng-Point to coordinates on the screen
	                GeoPoint point = item.getPoint();
	                Point ptScreenCoord = new Point();
	                mapView.getProjection().toPixels(point, ptScreenCoord);
	                
	                //Paint
	                Paint paint = new Paint();
	                paint.setTextAlign(Paint.Align.CENTER);
	                paint.setFakeBoldText(true);
	                paint.setTextSize(14);
	                paint.setARGB(150, 0, 0, 0); // alpha, r, g, b (Black, semi see-through)
	                
	                //show text to the right of the icon
	                canvas.drawText(item.getTitle(), ptScreenCoord.x, ptScreenCoord.y-20, paint);
	            }
	        }
	    }
	 
	 /**
	  * Changes the drawable(image) of a tag
	  * 
	  * @param drawable The drawable that you want to use.
	  * @param tag The tag on which you want to change drawable.
	  */
	public static void changeMarker(Drawable drawable, Tag tag){
		boundCenterBottom(drawable);
		tag.setMarker(drawable);
	}
	
	/**
	 * Removes a tag from the overlayslist.
	 * 
	 * @param tag Tag to be removed.
	 */
	public void removeTag(Tag tag){
		if(overlays.contains(tag)){
			overlays.remove(tag);
			setLastFocusedIndex(-1);
			populate();
		}
	}
	
}
